<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>20.5. xml.etree.ElementTree — The ElementTree XML API &mdash; Python 3.4.3 documentation</title>
    
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.4.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.4.3 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python 3.4.3 documentation" href="../index.html" />
    <link rel="up" title="20. Structured Markup Processing Tools" href="markup.html" />
    <link rel="next" title="20.6. xml.dom — The Document Object Model API" href="xml.dom.html" />
    <link rel="prev" title="20.4. XML Processing Modules" href="xml.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/version_switch.js"></script>
    
 

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="xml.dom.html" title="20.6. xml.dom — The Document Object Model API"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="xml.html" title="20.4. XML Processing Modules"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="markup.html" accesskey="U">20. Structured Markup Processing Tools</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-xml.etree.ElementTree">
<span id="xml-etree-elementtree-the-elementtree-xml-api"></span><h1>20.5. <a class="reference internal" href="#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><tt class="xref py py-mod docutils literal"><span class="pre">xml.etree.ElementTree</span></tt></a> &#8212; The ElementTree XML API<a class="headerlink" href="#module-xml.etree.ElementTree" title="Permalink to this headline">¶</a></h1>
<p>The <a class="reference internal" href="#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><tt class="xref py py-mod docutils literal"><span class="pre">xml.etree.ElementTree</span></tt></a> module implements a simple and efficient API
for parsing and creating XML data.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.3: </span>This module will use a fast implementation whenever available.
The <tt class="xref py py-mod docutils literal"><span class="pre">xml.etree.cElementTree</span></tt> module is deprecated.</p>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">The <a class="reference internal" href="#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><tt class="xref py py-mod docutils literal"><span class="pre">xml.etree.ElementTree</span></tt></a> module is not secure against
maliciously constructed data.  If you need to parse untrusted or
unauthenticated data see <a class="reference internal" href="xml.html#xml-vulnerabilities"><em>XML vulnerabilities</em></a>.</p>
</div>
<div class="section" id="tutorial">
<h2>20.5.1. Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h2>
<p>This is a short tutorial for using <a class="reference internal" href="#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><tt class="xref py py-mod docutils literal"><span class="pre">xml.etree.ElementTree</span></tt></a> (<tt class="docutils literal"><span class="pre">ET</span></tt> in
short).  The goal is to demonstrate some of the building blocks and basic
concepts of the module.</p>
<div class="section" id="xml-tree-and-elements">
<h3>20.5.1.1. XML tree and elements<a class="headerlink" href="#xml-tree-and-elements" title="Permalink to this headline">¶</a></h3>
<p>XML is an inherently hierarchical data format, and the most natural way to
represent it is with a tree.  <tt class="docutils literal"><span class="pre">ET</span></tt> has two classes for this purpose -
<a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><tt class="xref py py-class docutils literal"><span class="pre">ElementTree</span></tt></a> represents the whole XML document as a tree, and
<a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> represents a single node in this tree.  Interactions with
the whole document (reading and writing to/from files) are usually done
on the <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><tt class="xref py py-class docutils literal"><span class="pre">ElementTree</span></tt></a> level.  Interactions with a single XML element
and its sub-elements are done on the <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> level.</p>
</div>
<div class="section" id="parsing-xml">
<span id="elementtree-parsing-xml"></span><h3>20.5.1.2. Parsing XML<a class="headerlink" href="#parsing-xml" title="Permalink to this headline">¶</a></h3>
<p>We&#8217;ll be using the following XML document as the sample data for this section:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span>
<span class="nt">&lt;data&gt;</span>
    <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Liechtenstein&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;rank&gt;</span>1<span class="nt">&lt;/rank&gt;</span>
        <span class="nt">&lt;year&gt;</span>2008<span class="nt">&lt;/year&gt;</span>
        <span class="nt">&lt;gdppc&gt;</span>141100<span class="nt">&lt;/gdppc&gt;</span>
        <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Austria&quot;</span> <span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span>
        <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Switzerland&quot;</span> <span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;/country&gt;</span>
    <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Singapore&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;rank&gt;</span>4<span class="nt">&lt;/rank&gt;</span>
        <span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span>
        <span class="nt">&lt;gdppc&gt;</span>59900<span class="nt">&lt;/gdppc&gt;</span>
        <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Malaysia&quot;</span> <span class="na">direction=</span><span class="s">&quot;N&quot;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;/country&gt;</span>
    <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Panama&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;rank&gt;</span>68<span class="nt">&lt;/rank&gt;</span>
        <span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span>
        <span class="nt">&lt;gdppc&gt;</span>13600<span class="nt">&lt;/gdppc&gt;</span>
        <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Costa Rica&quot;</span> <span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span>
        <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Colombia&quot;</span> <span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;/country&gt;</span>
<span class="nt">&lt;/data&gt;</span>
</pre></div>
</div>
<p>We can import this data by reading from a file:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">xml.etree.ElementTree</span> <span class="k">as</span> <span class="nn">ET</span>
<span class="n">tree</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">&#39;country_data.xml&#39;</span><span class="p">)</span>
<span class="n">root</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">getroot</span><span class="p">()</span>
</pre></div>
</div>
<p>Or directly from a string:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">root</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">country_data_as_string</span><span class="p">)</span>
</pre></div>
</div>
<p><a class="reference internal" href="#xml.etree.ElementTree.fromstring" title="xml.etree.ElementTree.fromstring"><tt class="xref py py-func docutils literal"><span class="pre">fromstring()</span></tt></a> parses XML from a string directly into an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a>,
which is the root element of the parsed tree.  Other parsing functions may
create an <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><tt class="xref py py-class docutils literal"><span class="pre">ElementTree</span></tt></a>.  Check the documentation to be sure.</p>
<p>As an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a>, <tt class="docutils literal"><span class="pre">root</span></tt> has a tag and a dictionary of attributes:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span><span class="o">.</span><span class="n">tag</span>
<span class="go">&#39;data&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">root</span><span class="o">.</span><span class="n">attrib</span>
<span class="go">{}</span>
</pre></div>
</div>
<p>It also has children nodes over which we can iterate:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">child</span> <span class="ow">in</span> <span class="n">root</span><span class="p">:</span>
<span class="gp">... </span>  <span class="nb">print</span><span class="p">(</span><span class="n">child</span><span class="o">.</span><span class="n">tag</span><span class="p">,</span> <span class="n">child</span><span class="o">.</span><span class="n">attrib</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">country {&#39;name&#39;: &#39;Liechtenstein&#39;}</span>
<span class="go">country {&#39;name&#39;: &#39;Singapore&#39;}</span>
<span class="go">country {&#39;name&#39;: &#39;Panama&#39;}</span>
</pre></div>
</div>
<p>Children are nested, and we can access specific child nodes by index:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">text</span>
<span class="go">&#39;2008&#39;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Not all elements of the XML input will end up as elements of the
parsed tree. Currently, this module skips over any XML comments,
processing instructions, and document type declarations in the
input. Nevertheless, trees built using this module&#8217;s API rather
than parsing from XML text can have comments and processing
instructions in them; they will be included when generating XML
output. A document type declaration may be accessed by passing a
custom <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><tt class="xref py py-class docutils literal"><span class="pre">TreeBuilder</span></tt></a> instance to the <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a>
constructor.</p>
</div>
</div>
<div class="section" id="pull-api-for-non-blocking-parsing">
<span id="elementtree-pull-parsing"></span><h3>20.5.1.3. Pull API for non-blocking parsing<a class="headerlink" href="#pull-api-for-non-blocking-parsing" title="Permalink to this headline">¶</a></h3>
<p>Most parsing functions provided by this module require the whole document
to be read at once before returning any result.  It is possible to use an
<a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a> and feed data into it incrementally, but it is a push API that
calls methods on a callback target, which is too low-level and inconvenient for
most needs.  Sometimes what the user really wants is to be able to parse XML
incrementally, without blocking operations, while enjoying the convenience of
fully constructed <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> objects.</p>
<p>The most powerful tool for doing this is <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLPullParser</span></tt></a>.  It does not
require a blocking read to obtain the XML data, and is instead fed with data
incrementally with <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.feed" title="xml.etree.ElementTree.XMLPullParser.feed"><tt class="xref py py-meth docutils literal"><span class="pre">XMLPullParser.feed()</span></tt></a> calls.  To get the parsed XML
elements, call <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="xml.etree.ElementTree.XMLPullParser.read_events"><tt class="xref py py-meth docutils literal"><span class="pre">XMLPullParser.read_events()</span></tt></a>.  Here is an example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XMLPullParser</span><span class="p">([</span><span class="s">&#39;start&#39;</span><span class="p">,</span> <span class="s">&#39;end&#39;</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">feed</span><span class="p">(</span><span class="s">&#39;&lt;mytag&gt;sometext&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">read_events</span><span class="p">())</span>
<span class="go">[(&#39;start&#39;, &lt;Element &#39;mytag&#39; at 0x7fa66db2be58&gt;)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">feed</span><span class="p">(</span><span class="s">&#39; more text&lt;/mytag&gt;&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">event</span><span class="p">,</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">parser</span><span class="o">.</span><span class="n">read_events</span><span class="p">():</span>
<span class="gp">... </span>  <span class="nb">print</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
<span class="gp">... </span>  <span class="nb">print</span><span class="p">(</span><span class="n">elem</span><span class="o">.</span><span class="n">tag</span><span class="p">,</span> <span class="s">&#39;text=&#39;</span><span class="p">,</span> <span class="n">elem</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">end</span>
</pre></div>
</div>
<p>The obvious use case is applications that operate in a non-blocking fashion
where the XML data is being received from a socket or read incrementally from
some storage device.  In such cases, blocking reads are unacceptable.</p>
<p>Because it&#8217;s so flexible, <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLPullParser</span></tt></a> can be inconvenient to use for
simpler use-cases.  If you don&#8217;t mind your application blocking on reading XML
data but would still like to have incremental parsing capabilities, take a look
at <a class="reference internal" href="#xml.etree.ElementTree.iterparse" title="xml.etree.ElementTree.iterparse"><tt class="xref py py-func docutils literal"><span class="pre">iterparse()</span></tt></a>.  It can be useful when you&#8217;re reading a large XML document
and don&#8217;t want to hold it wholly in memory.</p>
</div>
<div class="section" id="finding-interesting-elements">
<h3>20.5.1.4. Finding interesting elements<a class="headerlink" href="#finding-interesting-elements" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> has some useful methods that help iterate recursively over all
the sub-tree below it (its children, their children, and so on).  For example,
<a class="reference internal" href="#xml.etree.ElementTree.Element.iter" title="xml.etree.ElementTree.Element.iter"><tt class="xref py py-meth docutils literal"><span class="pre">Element.iter()</span></tt></a>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">neighbor</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">iter</span><span class="p">(</span><span class="s">&#39;neighbor&#39;</span><span class="p">):</span>
<span class="gp">... </span>  <span class="nb">print</span><span class="p">(</span><span class="n">neighbor</span><span class="o">.</span><span class="n">attrib</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">{&#39;name&#39;: &#39;Austria&#39;, &#39;direction&#39;: &#39;E&#39;}</span>
<span class="go">{&#39;name&#39;: &#39;Switzerland&#39;, &#39;direction&#39;: &#39;W&#39;}</span>
<span class="go">{&#39;name&#39;: &#39;Malaysia&#39;, &#39;direction&#39;: &#39;N&#39;}</span>
<span class="go">{&#39;name&#39;: &#39;Costa Rica&#39;, &#39;direction&#39;: &#39;W&#39;}</span>
<span class="go">{&#39;name&#39;: &#39;Colombia&#39;, &#39;direction&#39;: &#39;E&#39;}</span>
</pre></div>
</div>
<p><a class="reference internal" href="#xml.etree.ElementTree.Element.findall" title="xml.etree.ElementTree.Element.findall"><tt class="xref py py-meth docutils literal"><span class="pre">Element.findall()</span></tt></a> finds only elements with a tag which are direct
children of the current element.  <a class="reference internal" href="#xml.etree.ElementTree.Element.find" title="xml.etree.ElementTree.Element.find"><tt class="xref py py-meth docutils literal"><span class="pre">Element.find()</span></tt></a> finds the <em>first</em> child
with a particular tag, and <a class="reference internal" href="#xml.etree.ElementTree.Element.text" title="xml.etree.ElementTree.Element.text"><tt class="xref py py-attr docutils literal"><span class="pre">Element.text</span></tt></a> accesses the element&#8217;s text
content.  <a class="reference internal" href="#xml.etree.ElementTree.Element.get" title="xml.etree.ElementTree.Element.get"><tt class="xref py py-meth docutils literal"><span class="pre">Element.get()</span></tt></a> accesses the element&#8217;s attributes:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">country</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">&#39;country&#39;</span><span class="p">):</span>
<span class="gp">... </span>  <span class="n">rank</span> <span class="o">=</span> <span class="n">country</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">&#39;rank&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">text</span>
<span class="gp">... </span>  <span class="n">name</span> <span class="o">=</span> <span class="n">country</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;name&#39;</span><span class="p">)</span>
<span class="gp">... </span>  <span class="nb">print</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">rank</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">Liechtenstein 1</span>
<span class="go">Singapore 4</span>
<span class="go">Panama 68</span>
</pre></div>
</div>
<p>More sophisticated specification of which elements to look for is possible by
using <a class="reference internal" href="#elementtree-xpath"><em>XPath</em></a>.</p>
</div>
<div class="section" id="modifying-an-xml-file">
<h3>20.5.1.5. Modifying an XML File<a class="headerlink" href="#modifying-an-xml-file" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><tt class="xref py py-class docutils literal"><span class="pre">ElementTree</span></tt></a> provides a simple way to build XML documents and write them to files.
The <a class="reference internal" href="#xml.etree.ElementTree.ElementTree.write" title="xml.etree.ElementTree.ElementTree.write"><tt class="xref py py-meth docutils literal"><span class="pre">ElementTree.write()</span></tt></a> method serves this purpose.</p>
<p>Once created, an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> object may be manipulated by directly changing
its fields (such as <a class="reference internal" href="#xml.etree.ElementTree.Element.text" title="xml.etree.ElementTree.Element.text"><tt class="xref py py-attr docutils literal"><span class="pre">Element.text</span></tt></a>), adding and modifying attributes
(<a class="reference internal" href="#xml.etree.ElementTree.Element.set" title="xml.etree.ElementTree.Element.set"><tt class="xref py py-meth docutils literal"><span class="pre">Element.set()</span></tt></a> method), as well as adding new children (for example
with <a class="reference internal" href="#xml.etree.ElementTree.Element.append" title="xml.etree.ElementTree.Element.append"><tt class="xref py py-meth docutils literal"><span class="pre">Element.append()</span></tt></a>).</p>
<p>Let&#8217;s say we want to add one to each country&#8217;s rank, and add an <tt class="docutils literal"><span class="pre">updated</span></tt>
attribute to the rank element:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">rank</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">iter</span><span class="p">(</span><span class="s">&#39;rank&#39;</span><span class="p">):</span>
<span class="gp">... </span>  <span class="n">new_rank</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">rank</span><span class="o">.</span><span class="n">text</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">... </span>  <span class="n">rank</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">new_rank</span><span class="p">)</span>
<span class="gp">... </span>  <span class="n">rank</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s">&#39;updated&#39;</span><span class="p">,</span> <span class="s">&#39;yes&#39;</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tree</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;output.xml&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Our XML now looks like this:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span>
<span class="nt">&lt;data&gt;</span>
    <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Liechtenstein&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;rank</span> <span class="na">updated=</span><span class="s">&quot;yes&quot;</span><span class="nt">&gt;</span>2<span class="nt">&lt;/rank&gt;</span>
        <span class="nt">&lt;year&gt;</span>2008<span class="nt">&lt;/year&gt;</span>
        <span class="nt">&lt;gdppc&gt;</span>141100<span class="nt">&lt;/gdppc&gt;</span>
        <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Austria&quot;</span> <span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span>
        <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Switzerland&quot;</span> <span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;/country&gt;</span>
    <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Singapore&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;rank</span> <span class="na">updated=</span><span class="s">&quot;yes&quot;</span><span class="nt">&gt;</span>5<span class="nt">&lt;/rank&gt;</span>
        <span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span>
        <span class="nt">&lt;gdppc&gt;</span>59900<span class="nt">&lt;/gdppc&gt;</span>
        <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Malaysia&quot;</span> <span class="na">direction=</span><span class="s">&quot;N&quot;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;/country&gt;</span>
    <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Panama&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;rank</span> <span class="na">updated=</span><span class="s">&quot;yes&quot;</span><span class="nt">&gt;</span>69<span class="nt">&lt;/rank&gt;</span>
        <span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span>
        <span class="nt">&lt;gdppc&gt;</span>13600<span class="nt">&lt;/gdppc&gt;</span>
        <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Costa Rica&quot;</span> <span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span>
        <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Colombia&quot;</span> <span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;/country&gt;</span>
<span class="nt">&lt;/data&gt;</span>
</pre></div>
</div>
<p>We can remove elements using <a class="reference internal" href="#xml.etree.ElementTree.Element.remove" title="xml.etree.ElementTree.Element.remove"><tt class="xref py py-meth docutils literal"><span class="pre">Element.remove()</span></tt></a>.  Let&#8217;s say we want to
remove all countries with a rank higher than 50:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">country</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">&#39;country&#39;</span><span class="p">):</span>
<span class="gp">... </span>  <span class="n">rank</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">country</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">&#39;rank&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
<span class="gp">... </span>  <span class="k">if</span> <span class="n">rank</span> <span class="o">&gt;</span> <span class="mi">50</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">root</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">country</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tree</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;output.xml&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Our XML now looks like this:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span>
<span class="nt">&lt;data&gt;</span>
    <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Liechtenstein&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;rank</span> <span class="na">updated=</span><span class="s">&quot;yes&quot;</span><span class="nt">&gt;</span>2<span class="nt">&lt;/rank&gt;</span>
        <span class="nt">&lt;year&gt;</span>2008<span class="nt">&lt;/year&gt;</span>
        <span class="nt">&lt;gdppc&gt;</span>141100<span class="nt">&lt;/gdppc&gt;</span>
        <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Austria&quot;</span> <span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span>
        <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Switzerland&quot;</span> <span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;/country&gt;</span>
    <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Singapore&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;rank</span> <span class="na">updated=</span><span class="s">&quot;yes&quot;</span><span class="nt">&gt;</span>5<span class="nt">&lt;/rank&gt;</span>
        <span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span>
        <span class="nt">&lt;gdppc&gt;</span>59900<span class="nt">&lt;/gdppc&gt;</span>
        <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Malaysia&quot;</span> <span class="na">direction=</span><span class="s">&quot;N&quot;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;/country&gt;</span>
<span class="nt">&lt;/data&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="building-xml-documents">
<h3>20.5.1.6. Building XML documents<a class="headerlink" href="#building-xml-documents" title="Permalink to this headline">¶</a></h3>
<p>The <a class="reference internal" href="#xml.etree.ElementTree.SubElement" title="xml.etree.ElementTree.SubElement"><tt class="xref py py-func docutils literal"><span class="pre">SubElement()</span></tt></a> function also provides a convenient way to create new
sub-elements for a given element:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">Element</span><span class="p">(</span><span class="s">&#39;a&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s">&#39;c&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="s">&#39;d&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ET</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">&lt;a&gt;&lt;b /&gt;&lt;c&gt;&lt;d /&gt;&lt;/c&gt;&lt;/a&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="parsing-xml-with-namespaces">
<h3>20.5.1.7. Parsing XML with Namespaces<a class="headerlink" href="#parsing-xml-with-namespaces" title="Permalink to this headline">¶</a></h3>
<p>If the XML input has <a class="reference external" href="https://en.wikipedia.org/wiki/XML_namespace">namespaces</a>, tags and attributes
with prefixes in the form <tt class="docutils literal"><span class="pre">prefix:sometag</span></tt> get expanded to
<tt class="docutils literal"><span class="pre">{uri}sometag</span></tt> where the <em>prefix</em> is replaced by the full <em>URI</em>.
Also, if there is a <a class="reference external" href="http://www.w3.org/TR/2006/REC-xml-names-20060816/#defaulting">default namespace</a>,
that full URI gets prepended to all of the non-prefixed tags.</p>
<p>Here is an XML example that incorporates two namespaces, one with the
prefix &#8220;fictional&#8221; and the other serving as the default namespace:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span>
<span class="nt">&lt;actors</span> <span class="na">xmlns:fictional=</span><span class="s">&quot;http://characters.example.com&quot;</span>
        <span class="na">xmlns=</span><span class="s">&quot;http://people.example.com&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;actor&gt;</span>
        <span class="nt">&lt;name&gt;</span>John Cleese<span class="nt">&lt;/name&gt;</span>
        <span class="nt">&lt;fictional:character&gt;</span>Lancelot<span class="nt">&lt;/fictional:character&gt;</span>
        <span class="nt">&lt;fictional:character&gt;</span>Archie Leach<span class="nt">&lt;/fictional:character&gt;</span>
    <span class="nt">&lt;/actor&gt;</span>
    <span class="nt">&lt;actor&gt;</span>
        <span class="nt">&lt;name&gt;</span>Eric Idle<span class="nt">&lt;/name&gt;</span>
        <span class="nt">&lt;fictional:character&gt;</span>Sir Robin<span class="nt">&lt;/fictional:character&gt;</span>
        <span class="nt">&lt;fictional:character&gt;</span>Gunther<span class="nt">&lt;/fictional:character&gt;</span>
        <span class="nt">&lt;fictional:character&gt;</span>Commander Clement<span class="nt">&lt;/fictional:character&gt;</span>
    <span class="nt">&lt;/actor&gt;</span>
<span class="nt">&lt;/actors&gt;</span>
</pre></div>
</div>
<p>One way to search and explore this XML example is to manually add the
URI to every tag or attribute in the xpath of a
<a class="reference internal" href="#xml.etree.ElementTree.Element.find" title="xml.etree.ElementTree.Element.find"><tt class="xref py py-meth docutils literal"><span class="pre">find()</span></tt></a> or <a class="reference internal" href="#xml.etree.ElementTree.Element.findall" title="xml.etree.ElementTree.Element.findall"><tt class="xref py py-meth docutils literal"><span class="pre">findall()</span></tt></a>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">root</span> <span class="o">=</span> <span class="n">fromstring</span><span class="p">(</span><span class="n">xml_text</span><span class="p">)</span>
<span class="k">for</span> <span class="n">actor</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">&#39;{http://people.example.com}actor&#39;</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">actor</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">&#39;{http://people.example.com}name&#39;</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">name</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">char</span> <span class="ow">in</span> <span class="n">actor</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">&#39;{http://characters.example.com}character&#39;</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="s">&#39; |--&gt;&#39;</span><span class="p">,</span> <span class="n">char</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
</pre></div>
</div>
<p>A better way to search the namespaced XML example is to create a
dictionary with your own prefixes and use those in the search functions:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">ns</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;real_person&#39;</span><span class="p">:</span> <span class="s">&#39;http://people.example.com&#39;</span><span class="p">,</span>
      <span class="s">&#39;role&#39;</span><span class="p">:</span> <span class="s">&#39;http://characters.example.com&#39;</span><span class="p">}</span>

<span class="k">for</span> <span class="n">actor</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">&#39;real_person:actor&#39;</span><span class="p">,</span> <span class="n">ns</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">actor</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">&#39;real_person:name&#39;</span><span class="p">,</span> <span class="n">ns</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">name</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">char</span> <span class="ow">in</span> <span class="n">actor</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">&#39;role:character&#39;</span><span class="p">,</span> <span class="n">ns</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="s">&#39; |--&gt;&#39;</span><span class="p">,</span> <span class="n">char</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
</pre></div>
</div>
<p>These two approaches both output:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">John</span> <span class="n">Cleese</span>
 <span class="o">|--&gt;</span> <span class="n">Lancelot</span>
 <span class="o">|--&gt;</span> <span class="n">Archie</span> <span class="n">Leach</span>
<span class="n">Eric</span> <span class="n">Idle</span>
 <span class="o">|--&gt;</span> <span class="n">Sir</span> <span class="n">Robin</span>
 <span class="o">|--&gt;</span> <span class="n">Gunther</span>
 <span class="o">|--&gt;</span> <span class="n">Commander</span> <span class="n">Clement</span>
</pre></div>
</div>
</div>
<div class="section" id="additional-resources">
<h3>20.5.1.8. Additional resources<a class="headerlink" href="#additional-resources" title="Permalink to this headline">¶</a></h3>
<p>See <a class="reference external" href="http://effbot.org/zone/element-index.htm">http://effbot.org/zone/element-index.htm</a> for tutorials and links to other
docs.</p>
</div>
</div>
<div class="section" id="xpath-support">
<span id="elementtree-xpath"></span><h2>20.5.2. XPath support<a class="headerlink" href="#xpath-support" title="Permalink to this headline">¶</a></h2>
<p>This module provides limited support for
<a class="reference external" href="http://www.w3.org/TR/xpath">XPath expressions</a> for locating elements in a
tree.  The goal is to support a small subset of the abbreviated syntax; a full
XPath engine is outside the scope of the module.</p>
<div class="section" id="example">
<h3>20.5.2.1. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h3>
<p>Here&#8217;s an example that demonstrates some of the XPath capabilities of the
module.  We&#8217;ll be using the <tt class="docutils literal"><span class="pre">countrydata</span></tt> XML document from the
<a class="reference internal" href="#elementtree-parsing-xml"><em>Parsing XML</em></a> section:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">xml.etree.ElementTree</span> <span class="k">as</span> <span class="nn">ET</span>

<span class="n">root</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">countrydata</span><span class="p">)</span>

<span class="c"># Top-level elements</span>
<span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">&quot;.&quot;</span><span class="p">)</span>

<span class="c"># All &#39;neighbor&#39; grand-children of &#39;country&#39; children of the top-level</span>
<span class="c"># elements</span>
<span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">&quot;./country/neighbor&quot;</span><span class="p">)</span>

<span class="c"># Nodes with name=&#39;Singapore&#39; that have a &#39;year&#39; child</span>
<span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">&quot;.//year/..[@name=&#39;Singapore&#39;]&quot;</span><span class="p">)</span>

<span class="c"># &#39;year&#39; nodes that are children of nodes with name=&#39;Singapore&#39;</span>
<span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">&quot;.//*[@name=&#39;Singapore&#39;]/year&quot;</span><span class="p">)</span>

<span class="c"># All &#39;neighbor&#39; nodes that are the second child of their parent</span>
<span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">&quot;.//neighbor[2]&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="supported-xpath-syntax">
<h3>20.5.2.2. Supported XPath syntax<a class="headerlink" href="#supported-xpath-syntax" title="Permalink to this headline">¶</a></h3>
<table border="1" class="docutils">
<colgroup>
<col width="30%" />
<col width="70%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Syntax</th>
<th class="head">Meaning</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">tag</span></tt></td>
<td>Selects all child elements with the given tag.
For example, <tt class="docutils literal"><span class="pre">spam</span></tt> selects all child elements
named <tt class="docutils literal"><span class="pre">spam</span></tt>, and <tt class="docutils literal"><span class="pre">spam/egg</span></tt> selects all
grandchildren named <tt class="docutils literal"><span class="pre">egg</span></tt> in all children named
<tt class="docutils literal"><span class="pre">spam</span></tt>.</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">*</span></tt></td>
<td>Selects all child elements.  For example, <tt class="docutils literal"><span class="pre">*/egg</span></tt>
selects all grandchildren named <tt class="docutils literal"><span class="pre">egg</span></tt>.</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">.</span></tt></td>
<td>Selects the current node.  This is mostly useful
at the beginning of the path, to indicate that it&#8217;s
a relative path.</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">//</span></tt></td>
<td>Selects all subelements, on all levels beneath the
current  element.  For example, <tt class="docutils literal"><span class="pre">.//egg</span></tt> selects
all <tt class="docutils literal"><span class="pre">egg</span></tt> elements in the entire tree.</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">..</span></tt></td>
<td>Selects the parent element.  Returns <tt class="docutils literal"><span class="pre">None</span></tt> if the
path attempts to reach the ancestors of the start
element (the element <tt class="docutils literal"><span class="pre">find</span></tt> was called on).</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">[&#64;attrib]</span></tt></td>
<td>Selects all elements that have the given attribute.</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">[&#64;attrib='value']</span></tt></td>
<td>Selects all elements for which the given attribute
has the given value.  The value cannot contain
quotes.</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">[tag]</span></tt></td>
<td>Selects all elements that have a child named
<tt class="docutils literal"><span class="pre">tag</span></tt>.  Only immediate children are supported.</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">[tag='text']</span></tt></td>
<td>Selects all elements that have a child named
<tt class="docutils literal"><span class="pre">tag</span></tt> whose complete text content, including
descendants, equals the given <tt class="docutils literal"><span class="pre">text</span></tt>.</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">[position]</span></tt></td>
<td>Selects all elements that are located at the given
position.  The position can be either an integer
(1 is the first position), the expression <tt class="docutils literal"><span class="pre">last()</span></tt>
(for the last position), or a position relative to
the last position (e.g. <tt class="docutils literal"><span class="pre">last()-1</span></tt>).</td>
</tr>
</tbody>
</table>
<p>Predicates (expressions within square brackets) must be preceded by a tag
name, an asterisk, or another predicate.  <tt class="docutils literal"><span class="pre">position</span></tt> predicates must be
preceded by a tag name.</p>
</div>
</div>
<div class="section" id="reference">
<h2>20.5.3. Reference<a class="headerlink" href="#reference" title="Permalink to this headline">¶</a></h2>
<div class="section" id="functions">
<span id="elementtree-functions"></span><h3>20.5.3.1. Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="xml.etree.ElementTree.Comment">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">Comment</tt><big>(</big><em>text=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Comment" title="Permalink to this definition">¶</a></dt>
<dd><p>Comment element factory.  This factory function creates a special element
that will be serialized as an XML comment by the standard serializer.  The
comment string can be either a bytestring or a Unicode string.  <em>text</em> is a
string containing the comment string.  Returns an element instance
representing a comment.</p>
<p>Note that <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a> skips over comments in the input
instead of creating comment objects for them. An <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><tt class="xref py py-class docutils literal"><span class="pre">ElementTree</span></tt></a> will
only contain comment nodes if they have been inserted into to
the tree using one of the <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> methods.</p>
</dd></dl>

<dl class="function">
<dt id="xml.etree.ElementTree.dump">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">dump</tt><big>(</big><em>elem</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.dump" title="Permalink to this definition">¶</a></dt>
<dd><p>Writes an element tree or element structure to sys.stdout.  This function
should be used for debugging only.</p>
<p>The exact output format is implementation dependent.  In this version, it&#8217;s
written as an ordinary XML file.</p>
<p><em>elem</em> is an element tree or an individual element.</p>
</dd></dl>

<dl class="function">
<dt id="xml.etree.ElementTree.fromstring">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">fromstring</tt><big>(</big><em>text</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.fromstring" title="Permalink to this definition">¶</a></dt>
<dd><p>Parses an XML section from a string constant.  Same as <a class="reference internal" href="#xml.etree.ElementTree.XML" title="xml.etree.ElementTree.XML"><tt class="xref py py-func docutils literal"><span class="pre">XML()</span></tt></a>.  <em>text</em>
is a string containing XML data.  Returns an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> instance.</p>
</dd></dl>

<dl class="function">
<dt id="xml.etree.ElementTree.fromstringlist">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">fromstringlist</tt><big>(</big><em>sequence</em>, <em>parser=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.fromstringlist" title="Permalink to this definition">¶</a></dt>
<dd><p>Parses an XML document from a sequence of string fragments.  <em>sequence</em> is a
list or other sequence containing XML data fragments.  <em>parser</em> is an
optional parser instance.  If not given, the standard <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a>
parser is used.  Returns an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> instance.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="xml.etree.ElementTree.iselement">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">iselement</tt><big>(</big><em>element</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.iselement" title="Permalink to this definition">¶</a></dt>
<dd><p>Checks if an object appears to be a valid element object.  <em>element</em> is an
element instance.  Returns a true value if this is an element object.</p>
</dd></dl>

<dl class="function">
<dt id="xml.etree.ElementTree.iterparse">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">iterparse</tt><big>(</big><em>source</em>, <em>events=None</em>, <em>parser=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.iterparse" title="Permalink to this definition">¶</a></dt>
<dd><p>Parses an XML section into an element tree incrementally, and reports what&#8217;s
going on to the user.  <em>source</em> is a filename or <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file object</em></a>
containing XML data.  <em>events</em> is a sequence of events to report back.  The
supported events are the strings <tt class="docutils literal"><span class="pre">&quot;start&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;end&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;start-ns&quot;</span></tt> and
<tt class="docutils literal"><span class="pre">&quot;end-ns&quot;</span></tt> (the &#8220;ns&#8221; events are used to get detailed namespace
information).  If <em>events</em> is omitted, only <tt class="docutils literal"><span class="pre">&quot;end&quot;</span></tt> events are reported.
<em>parser</em> is an optional parser instance.  If not given, the standard
<a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a> parser is used.  <em>parser</em> must be a subclass of
<a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a> and can only use the default <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><tt class="xref py py-class docutils literal"><span class="pre">TreeBuilder</span></tt></a> as a
target.  Returns an <a class="reference internal" href="../glossary.html#term-iterator"><em class="xref std std-term">iterator</em></a> providing <tt class="docutils literal"><span class="pre">(event,</span> <span class="pre">elem)</span></tt> pairs.</p>
<p>Note that while <a class="reference internal" href="#xml.etree.ElementTree.iterparse" title="xml.etree.ElementTree.iterparse"><tt class="xref py py-func docutils literal"><span class="pre">iterparse()</span></tt></a> builds the tree incrementally, it issues
blocking reads on <em>source</em> (or the file it names).  As such, it&#8217;s unsuitable
for applications where blocking reads can&#8217;t be made.  For fully non-blocking
parsing, see <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLPullParser</span></tt></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p><a class="reference internal" href="#xml.etree.ElementTree.iterparse" title="xml.etree.ElementTree.iterparse"><tt class="xref py py-func docutils literal"><span class="pre">iterparse()</span></tt></a> only guarantees that it has seen the &#8220;&gt;&#8221; character of a
starting tag when it emits a &#8220;start&#8221; event, so the attributes are defined,
but the contents of the text and tail attributes are undefined at that
point.  The same applies to the element children; they may or may not be
present.</p>
<p class="last">If you need a fully populated element, look for &#8220;end&#8221; events instead.</p>
</div>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 3.4: </span>The <em>parser</em> argument.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="xml.etree.ElementTree.parse">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">parse</tt><big>(</big><em>source</em>, <em>parser=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.parse" title="Permalink to this definition">¶</a></dt>
<dd><p>Parses an XML section into an element tree.  <em>source</em> is a filename or file
object containing XML data.  <em>parser</em> is an optional parser instance.  If
not given, the standard <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a> parser is used.  Returns an
<a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><tt class="xref py py-class docutils literal"><span class="pre">ElementTree</span></tt></a> instance.</p>
</dd></dl>

<dl class="function">
<dt id="xml.etree.ElementTree.ProcessingInstruction">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">ProcessingInstruction</tt><big>(</big><em>target</em>, <em>text=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.ProcessingInstruction" title="Permalink to this definition">¶</a></dt>
<dd><p>PI element factory.  This factory function creates a special element that
will be serialized as an XML processing instruction.  <em>target</em> is a string
containing the PI target.  <em>text</em> is a string containing the PI contents, if
given.  Returns an element instance, representing a processing instruction.</p>
<p>Note that <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a> skips over processing instructions
in the input instead of creating comment objects for them. An
<a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><tt class="xref py py-class docutils literal"><span class="pre">ElementTree</span></tt></a> will only contain processing instruction nodes if
they have been inserted into to the tree using one of the
<a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> methods.</p>
</dd></dl>

<dl class="function">
<dt id="xml.etree.ElementTree.register_namespace">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">register_namespace</tt><big>(</big><em>prefix</em>, <em>uri</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.register_namespace" title="Permalink to this definition">¶</a></dt>
<dd><p>Registers a namespace prefix.  The registry is global, and any existing
mapping for either the given prefix or the namespace URI will be removed.
<em>prefix</em> is a namespace prefix.  <em>uri</em> is a namespace uri.  Tags and
attributes in this namespace will be serialized with the given prefix, if at
all possible.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="xml.etree.ElementTree.SubElement">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">SubElement</tt><big>(</big><em>parent</em>, <em>tag</em>, <em>attrib={}</em>, <em>**extra</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.SubElement" title="Permalink to this definition">¶</a></dt>
<dd><p>Subelement factory.  This function creates an element instance, and appends
it to an existing element.</p>
<p>The element name, attribute names, and attribute values can be either
bytestrings or Unicode strings.  <em>parent</em> is the parent element.  <em>tag</em> is
the subelement name.  <em>attrib</em> is an optional dictionary, containing element
attributes.  <em>extra</em> contains additional attributes, given as keyword
arguments.  Returns an element instance.</p>
</dd></dl>

<dl class="function">
<dt id="xml.etree.ElementTree.tostring">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">tostring</tt><big>(</big><em>element</em>, <em>encoding=&quot;us-ascii&quot;</em>, <em>method=&quot;xml&quot;</em>, <em>*</em>, <em>short_empty_elements=True</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.tostring" title="Permalink to this definition">¶</a></dt>
<dd><p>Generates a string representation of an XML element, including all
subelements.  <em>element</em> is an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> instance.  <em>encoding</em> <a class="footnote-reference" href="#id5" id="id1">[1]</a> is
the output encoding (default is US-ASCII).  Use <tt class="docutils literal"><span class="pre">encoding=&quot;unicode&quot;</span></tt> to
generate a Unicode string (otherwise, a bytestring is generated).  <em>method</em>
is either <tt class="docutils literal"><span class="pre">&quot;xml&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;html&quot;</span></tt> or <tt class="docutils literal"><span class="pre">&quot;text&quot;</span></tt> (default is <tt class="docutils literal"><span class="pre">&quot;xml&quot;</span></tt>).
<em>short_empty_elements</em> has the same meaning as in <a class="reference internal" href="#xml.etree.ElementTree.ElementTree.write" title="xml.etree.ElementTree.ElementTree.write"><tt class="xref py py-meth docutils literal"><span class="pre">ElementTree.write()</span></tt></a>.
Returns an (optionally) encoded string containing the XML data.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4: </span>The <em>short_empty_elements</em> parameter.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="xml.etree.ElementTree.tostringlist">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">tostringlist</tt><big>(</big><em>element</em>, <em>encoding=&quot;us-ascii&quot;</em>, <em>method=&quot;xml&quot;</em>, <em>*</em>, <em>short_empty_elements=True</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.tostringlist" title="Permalink to this definition">¶</a></dt>
<dd><p>Generates a string representation of an XML element, including all
subelements.  <em>element</em> is an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> instance.  <em>encoding</em> <a class="footnote-reference" href="#id5" id="id2">[1]</a> is
the output encoding (default is US-ASCII).  Use <tt class="docutils literal"><span class="pre">encoding=&quot;unicode&quot;</span></tt> to
generate a Unicode string (otherwise, a bytestring is generated).  <em>method</em>
is either <tt class="docutils literal"><span class="pre">&quot;xml&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;html&quot;</span></tt> or <tt class="docutils literal"><span class="pre">&quot;text&quot;</span></tt> (default is <tt class="docutils literal"><span class="pre">&quot;xml&quot;</span></tt>).
<em>short_empty_elements</em> has the same meaning as in <a class="reference internal" href="#xml.etree.ElementTree.ElementTree.write" title="xml.etree.ElementTree.ElementTree.write"><tt class="xref py py-meth docutils literal"><span class="pre">ElementTree.write()</span></tt></a>.
Returns a list of (optionally) encoded strings containing the XML data.
It does not guarantee any specific sequence, except that
<tt class="docutils literal"><span class="pre">b&quot;&quot;.join(tostringlist(element))</span> <span class="pre">==</span> <span class="pre">tostring(element)</span></tt>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4: </span>The <em>short_empty_elements</em> parameter.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="xml.etree.ElementTree.XML">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">XML</tt><big>(</big><em>text</em>, <em>parser=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.XML" title="Permalink to this definition">¶</a></dt>
<dd><p>Parses an XML section from a string constant.  This function can be used to
embed &#8220;XML literals&#8221; in Python code.  <em>text</em> is a string containing XML
data.  <em>parser</em> is an optional parser instance.  If not given, the standard
<a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a> parser is used.  Returns an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> instance.</p>
</dd></dl>

<dl class="function">
<dt id="xml.etree.ElementTree.XMLID">
<tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">XMLID</tt><big>(</big><em>text</em>, <em>parser=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.XMLID" title="Permalink to this definition">¶</a></dt>
<dd><p>Parses an XML section from a string constant, and also returns a dictionary
which maps from element id:s to elements.  <em>text</em> is a string containing XML
data.  <em>parser</em> is an optional parser instance.  If not given, the standard
<a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a> parser is used.  Returns a tuple containing an
<a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> instance and a dictionary.</p>
</dd></dl>

</div>
<div class="section" id="element-objects">
<span id="elementtree-element-objects"></span><h3>20.5.3.2. Element Objects<a class="headerlink" href="#element-objects" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="xml.etree.ElementTree.Element">
<em class="property">class </em><tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">Element</tt><big>(</big><em>tag</em>, <em>attrib={}</em>, <em>**extra</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element" title="Permalink to this definition">¶</a></dt>
<dd><p>Element class.  This class defines the Element interface, and provides a
reference implementation of this interface.</p>
<p>The element name, attribute names, and attribute values can be either
bytestrings or Unicode strings.  <em>tag</em> is the element name.  <em>attrib</em> is
an optional dictionary, containing element attributes.  <em>extra</em> contains
additional attributes, given as keyword arguments.</p>
<dl class="attribute">
<dt id="xml.etree.ElementTree.Element.tag">
<tt class="descname">tag</tt><a class="headerlink" href="#xml.etree.ElementTree.Element.tag" title="Permalink to this definition">¶</a></dt>
<dd><p>A string identifying what kind of data this element represents (the
element type, in other words).</p>
</dd></dl>

<dl class="attribute">
<dt id="xml.etree.ElementTree.Element.text">
<tt class="descname">text</tt><a class="headerlink" href="#xml.etree.ElementTree.Element.text" title="Permalink to this definition">¶</a></dt>
<dt id="xml.etree.ElementTree.Element.tail">
<tt class="descname">tail</tt><a class="headerlink" href="#xml.etree.ElementTree.Element.tail" title="Permalink to this definition">¶</a></dt>
<dd><p>These attributes can be used to hold additional data associated with
the element.  Their values are usually strings but may be any
application-specific object.  If the element is created from
an XML file, the <em>text</em> attribute holds either the text between
the element&#8217;s start tag and its first child or end tag, or <tt class="docutils literal"><span class="pre">None</span></tt>, and
the <em>tail</em> attribute holds either the text between the element&#8217;s
end tag and the next tag, or <tt class="docutils literal"><span class="pre">None</span></tt>.  For the XML data</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;a&gt;&lt;b&gt;</span>1<span class="nt">&lt;c&gt;</span>2<span class="nt">&lt;d/&gt;</span>3<span class="nt">&lt;/c&gt;&lt;/b&gt;</span>4<span class="nt">&lt;/a&gt;</span>
</pre></div>
</div>
<p>the <em>a</em> element has <tt class="docutils literal"><span class="pre">None</span></tt> for both <em>text</em> and <em>tail</em> attributes,
the <em>b</em> element has <em>text</em> <tt class="docutils literal"><span class="pre">&quot;1&quot;</span></tt> and <em>tail</em> <tt class="docutils literal"><span class="pre">&quot;4&quot;</span></tt>,
the <em>c</em> element has <em>text</em> <tt class="docutils literal"><span class="pre">&quot;2&quot;</span></tt> and <em>tail</em> <tt class="docutils literal"><span class="pre">None</span></tt>,
and the <em>d</em> element has <em>text</em> <tt class="docutils literal"><span class="pre">None</span></tt> and <em>tail</em> <tt class="docutils literal"><span class="pre">&quot;3&quot;</span></tt>.</p>
<p>To collect the inner text of an element, see <a class="reference internal" href="#xml.etree.ElementTree.Element.itertext" title="xml.etree.ElementTree.Element.itertext"><tt class="xref py py-meth docutils literal"><span class="pre">itertext()</span></tt></a>, for
example <tt class="docutils literal"><span class="pre">&quot;&quot;.join(element.itertext())</span></tt>.</p>
<p>Applications may store arbitrary objects in these attributes.</p>
</dd></dl>

<dl class="attribute">
<dt id="xml.etree.ElementTree.Element.attrib">
<tt class="descname">attrib</tt><a class="headerlink" href="#xml.etree.ElementTree.Element.attrib" title="Permalink to this definition">¶</a></dt>
<dd><p>A dictionary containing the element&#8217;s attributes.  Note that while the
<em>attrib</em> value is always a real mutable Python dictionary, an ElementTree
implementation may choose to use another internal representation, and
create the dictionary only if someone asks for it.  To take advantage of
such implementations, use the dictionary methods below whenever possible.</p>
</dd></dl>

<p>The following dictionary-like methods work on the element attributes.</p>
<dl class="method">
<dt id="xml.etree.ElementTree.Element.clear">
<tt class="descname">clear</tt><big>(</big><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.clear" title="Permalink to this definition">¶</a></dt>
<dd><p>Resets an element.  This function removes all subelements, clears all
attributes, and sets the text and tail attributes to <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.get">
<tt class="descname">get</tt><big>(</big><em>key</em>, <em>default=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.get" title="Permalink to this definition">¶</a></dt>
<dd><p>Gets the element attribute named <em>key</em>.</p>
<p>Returns the attribute value, or <em>default</em> if the attribute was not found.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.items">
<tt class="descname">items</tt><big>(</big><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.items" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the element attributes as a sequence of (name, value) pairs.  The
attributes are returned in an arbitrary order.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.keys">
<tt class="descname">keys</tt><big>(</big><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.keys" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the elements attribute names as a list.  The names are returned
in an arbitrary order.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.set">
<tt class="descname">set</tt><big>(</big><em>key</em>, <em>value</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.set" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the attribute <em>key</em> on the element to <em>value</em>.</p>
</dd></dl>

<p>The following methods work on the element&#8217;s children (subelements).</p>
<dl class="method">
<dt id="xml.etree.ElementTree.Element.append">
<tt class="descname">append</tt><big>(</big><em>subelement</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.append" title="Permalink to this definition">¶</a></dt>
<dd><p>Adds the element <em>subelement</em> to the end of this element&#8217;s internal list
of subelements.  Raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> if <em>subelement</em> is not an
<a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.extend">
<tt class="descname">extend</tt><big>(</big><em>subelements</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.extend" title="Permalink to this definition">¶</a></dt>
<dd><p>Appends <em>subelements</em> from a sequence object with zero or more elements.
Raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> if a subelement is not an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.find">
<tt class="descname">find</tt><big>(</big><em>match</em>, <em>namespaces=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.find" title="Permalink to this definition">¶</a></dt>
<dd><p>Finds the first subelement matching <em>match</em>.  <em>match</em> may be a tag name
or a <a class="reference internal" href="#elementtree-xpath"><em>path</em></a>.  Returns an element instance
or <tt class="docutils literal"><span class="pre">None</span></tt>.  <em>namespaces</em> is an optional mapping from namespace prefix
to full name.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.findall">
<tt class="descname">findall</tt><big>(</big><em>match</em>, <em>namespaces=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.findall" title="Permalink to this definition">¶</a></dt>
<dd><p>Finds all matching subelements, by tag name or
<a class="reference internal" href="#elementtree-xpath"><em>path</em></a>.  Returns a list containing all matching
elements in document order.  <em>namespaces</em> is an optional mapping from
namespace prefix to full name.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.findtext">
<tt class="descname">findtext</tt><big>(</big><em>match</em>, <em>default=None</em>, <em>namespaces=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.findtext" title="Permalink to this definition">¶</a></dt>
<dd><p>Finds text for the first subelement matching <em>match</em>.  <em>match</em> may be
a tag name or a <a class="reference internal" href="#elementtree-xpath"><em>path</em></a>.  Returns the text content
of the first matching element, or <em>default</em> if no element was found.
Note that if the matching element has no text content an empty string
is returned. <em>namespaces</em> is an optional mapping from namespace prefix
to full name.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.getchildren">
<tt class="descname">getchildren</tt><big>(</big><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.getchildren" title="Permalink to this definition">¶</a></dt>
<dd><div class="deprecated">
<p><span class="versionmodified">Deprecated since version 3.2: </span>Use <tt class="docutils literal"><span class="pre">list(elem)</span></tt> or iteration.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.getiterator">
<tt class="descname">getiterator</tt><big>(</big><em>tag=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.getiterator" title="Permalink to this definition">¶</a></dt>
<dd><div class="deprecated">
<p><span class="versionmodified">Deprecated since version 3.2: </span>Use method <a class="reference internal" href="#xml.etree.ElementTree.Element.iter" title="xml.etree.ElementTree.Element.iter"><tt class="xref py py-meth docutils literal"><span class="pre">Element.iter()</span></tt></a> instead.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.insert">
<tt class="descname">insert</tt><big>(</big><em>index</em>, <em>subelement</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.insert" title="Permalink to this definition">¶</a></dt>
<dd><p>Inserts <em>subelement</em> at the given position in this element.  Raises
<a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> if <em>subelement</em> is not an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.iter">
<tt class="descname">iter</tt><big>(</big><em>tag=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.iter" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a tree <a class="reference internal" href="../glossary.html#term-iterator"><em class="xref std std-term">iterator</em></a> with the current element as the root.
The iterator iterates over this element and all elements below it, in
document (depth first) order.  If <em>tag</em> is not <tt class="docutils literal"><span class="pre">None</span></tt> or <tt class="docutils literal"><span class="pre">'*'</span></tt>, only
elements whose tag equals <em>tag</em> are returned from the iterator.  If the
tree structure is modified during iteration, the result is undefined.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.iterfind">
<tt class="descname">iterfind</tt><big>(</big><em>match</em>, <em>namespaces=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.iterfind" title="Permalink to this definition">¶</a></dt>
<dd><p>Finds all matching subelements, by tag name or
<a class="reference internal" href="#elementtree-xpath"><em>path</em></a>.  Returns an iterable yielding all
matching elements in document order. <em>namespaces</em> is an optional mapping
from namespace prefix to full name.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.itertext">
<tt class="descname">itertext</tt><big>(</big><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.itertext" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a text iterator.  The iterator loops over this element and all
subelements, in document order, and returns all inner text.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.makeelement">
<tt class="descname">makeelement</tt><big>(</big><em>tag</em>, <em>attrib</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.makeelement" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a new element object of the same type as this element.  Do not
call this method, use the <a class="reference internal" href="#xml.etree.ElementTree.SubElement" title="xml.etree.ElementTree.SubElement"><tt class="xref py py-func docutils literal"><span class="pre">SubElement()</span></tt></a> factory function instead.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.Element.remove">
<tt class="descname">remove</tt><big>(</big><em>subelement</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.Element.remove" title="Permalink to this definition">¶</a></dt>
<dd><p>Removes <em>subelement</em> from the element.  Unlike the find* methods this
method compares elements based on the instance identity, not on tag value
or contents.</p>
</dd></dl>

<p><a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> objects also support the following sequence type methods
for working with subelements: <a class="reference internal" href="../reference/datamodel.html#object.__delitem__" title="object.__delitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__delitem__()</span></tt></a>,
<a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a>, <a class="reference internal" href="../reference/datamodel.html#object.__setitem__" title="object.__setitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__setitem__()</span></tt></a>,
<a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><tt class="xref py py-meth docutils literal"><span class="pre">__len__()</span></tt></a>.</p>
<p>Caution: Elements with no subelements will test as <tt class="docutils literal"><span class="pre">False</span></tt>.  This behavior
will change in future versions.  Use specific <tt class="docutils literal"><span class="pre">len(elem)</span></tt> or <tt class="docutils literal"><span class="pre">elem</span> <span class="pre">is</span>
<span class="pre">None</span></tt> test instead.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">element</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">&#39;foo&#39;</span><span class="p">)</span>

<span class="k">if</span> <span class="ow">not</span> <span class="n">element</span><span class="p">:</span>  <span class="c"># careful!</span>
    <span class="nb">print</span><span class="p">(</span><span class="s">&quot;element not found, or element has no subelements&quot;</span><span class="p">)</span>

<span class="k">if</span> <span class="n">element</span> <span class="ow">is</span> <span class="k">None</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="s">&quot;element not found&quot;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="elementtree-objects">
<span id="elementtree-elementtree-objects"></span><h3>20.5.3.3. ElementTree Objects<a class="headerlink" href="#elementtree-objects" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="xml.etree.ElementTree.ElementTree">
<em class="property">class </em><tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">ElementTree</tt><big>(</big><em>element=None</em>, <em>file=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.ElementTree" title="Permalink to this definition">¶</a></dt>
<dd><p>ElementTree wrapper class.  This class represents an entire element
hierarchy, and adds some extra support for serialization to and from
standard XML.</p>
<p><em>element</em> is the root element.  The tree is initialized with the contents
of the XML <em>file</em> if given.</p>
<dl class="method">
<dt id="xml.etree.ElementTree.ElementTree._setroot">
<tt class="descname">_setroot</tt><big>(</big><em>element</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.ElementTree._setroot" title="Permalink to this definition">¶</a></dt>
<dd><p>Replaces the root element for this tree.  This discards the current
contents of the tree, and replaces it with the given element.  Use with
care.  <em>element</em> is an element instance.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.ElementTree.find">
<tt class="descname">find</tt><big>(</big><em>match</em>, <em>namespaces=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.find" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as <a class="reference internal" href="#xml.etree.ElementTree.Element.find" title="xml.etree.ElementTree.Element.find"><tt class="xref py py-meth docutils literal"><span class="pre">Element.find()</span></tt></a>, starting at the root of the tree.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.ElementTree.findall">
<tt class="descname">findall</tt><big>(</big><em>match</em>, <em>namespaces=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.findall" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as <a class="reference internal" href="#xml.etree.ElementTree.Element.findall" title="xml.etree.ElementTree.Element.findall"><tt class="xref py py-meth docutils literal"><span class="pre">Element.findall()</span></tt></a>, starting at the root of the tree.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.ElementTree.findtext">
<tt class="descname">findtext</tt><big>(</big><em>match</em>, <em>default=None</em>, <em>namespaces=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.findtext" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as <a class="reference internal" href="#xml.etree.ElementTree.Element.findtext" title="xml.etree.ElementTree.Element.findtext"><tt class="xref py py-meth docutils literal"><span class="pre">Element.findtext()</span></tt></a>, starting at the root of the tree.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.ElementTree.getiterator">
<tt class="descname">getiterator</tt><big>(</big><em>tag=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.getiterator" title="Permalink to this definition">¶</a></dt>
<dd><div class="deprecated">
<p><span class="versionmodified">Deprecated since version 3.2: </span>Use method <a class="reference internal" href="#xml.etree.ElementTree.ElementTree.iter" title="xml.etree.ElementTree.ElementTree.iter"><tt class="xref py py-meth docutils literal"><span class="pre">ElementTree.iter()</span></tt></a> instead.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.ElementTree.getroot">
<tt class="descname">getroot</tt><big>(</big><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.getroot" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the root element for this tree.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.ElementTree.iter">
<tt class="descname">iter</tt><big>(</big><em>tag=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.iter" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates and returns a tree iterator for the root element.  The iterator
loops over all elements in this tree, in section order.  <em>tag</em> is the tag
to look for (default is to return all elements)</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.ElementTree.iterfind">
<tt class="descname">iterfind</tt><big>(</big><em>match</em>, <em>namespaces=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.iterfind" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as <a class="reference internal" href="#xml.etree.ElementTree.Element.iterfind" title="xml.etree.ElementTree.Element.iterfind"><tt class="xref py py-meth docutils literal"><span class="pre">Element.iterfind()</span></tt></a>, starting at the root of the tree.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.ElementTree.parse">
<tt class="descname">parse</tt><big>(</big><em>source</em>, <em>parser=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.parse" title="Permalink to this definition">¶</a></dt>
<dd><p>Loads an external XML section into this element tree.  <em>source</em> is a file
name or <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file object</em></a>.  <em>parser</em> is an optional parser instance.
If not given, the standard <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a> parser is used.  Returns the
section root element.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.ElementTree.write">
<tt class="descname">write</tt><big>(</big><em>file</em>, <em>encoding=&quot;us-ascii&quot;</em>, <em>xml_declaration=None</em>, <em>default_namespace=None</em>, <em>method=&quot;xml&quot;</em>, <em>*</em>, <em>short_empty_elements=True</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.write" title="Permalink to this definition">¶</a></dt>
<dd><p>Writes the element tree to a file, as XML.  <em>file</em> is a file name, or a
<a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file object</em></a> opened for writing.  <em>encoding</em> <a class="footnote-reference" href="#id5" id="id3">[1]</a> is the output
encoding (default is US-ASCII).
<em>xml_declaration</em> controls if an XML declaration should be added to the
file.  Use <tt class="docutils literal"><span class="pre">False</span></tt> for never, <tt class="docutils literal"><span class="pre">True</span></tt> for always, <tt class="docutils literal"><span class="pre">None</span></tt>
for only if not US-ASCII or UTF-8 or Unicode (default is <tt class="docutils literal"><span class="pre">None</span></tt>).
<em>default_namespace</em> sets the default XML namespace (for &#8220;xmlns&#8221;).
<em>method</em> is either <tt class="docutils literal"><span class="pre">&quot;xml&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;html&quot;</span></tt> or <tt class="docutils literal"><span class="pre">&quot;text&quot;</span></tt> (default is
<tt class="docutils literal"><span class="pre">&quot;xml&quot;</span></tt>).
The keyword-only <em>short_empty_elements</em> parameter controls the formatting
of elements that contain no content.  If <em>True</em> (the default), they are
emitted as a single self-closed tag, otherwise they are emitted as a pair
of start/end tags.</p>
<p>The output is either a string (<a class="reference internal" href="stdtypes.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a>) or binary (<a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a>).
This is controlled by the <em>encoding</em> argument.  If <em>encoding</em> is
<tt class="docutils literal"><span class="pre">&quot;unicode&quot;</span></tt>, the output is a string; otherwise, it&#8217;s binary.  Note that
this may conflict with the type of <em>file</em> if it&#8217;s an open
<a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file object</em></a>; make sure you do not try to write a string to a
binary stream and vice versa.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4: </span>The <em>short_empty_elements</em> parameter.</p>
</div>
</dd></dl>

</dd></dl>

<p>This is the XML file that is going to be manipulated:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="o">&lt;</span><span class="n">html</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">head</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">title</span><span class="o">&gt;</span><span class="n">Example</span> <span class="n">page</span><span class="o">&lt;/</span><span class="n">title</span><span class="o">&gt;</span>
    <span class="o">&lt;/</span><span class="n">head</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">body</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">p</span><span class="o">&gt;</span><span class="n">Moved</span> <span class="n">to</span> <span class="o">&lt;</span><span class="n">a</span> <span class="n">href</span><span class="o">=</span><span class="s">&quot;http://example.org/&quot;</span><span class="o">&gt;</span><span class="n">example</span><span class="o">.</span><span class="n">org</span><span class="o">&lt;/</span><span class="n">a</span><span class="o">&gt;</span>
        <span class="ow">or</span> <span class="o">&lt;</span><span class="n">a</span> <span class="n">href</span><span class="o">=</span><span class="s">&quot;http://example.com/&quot;</span><span class="o">&gt;</span><span class="n">example</span><span class="o">.</span><span class="n">com</span><span class="o">&lt;/</span><span class="n">a</span><span class="o">&gt;.&lt;/</span><span class="n">p</span><span class="o">&gt;</span>
    <span class="o">&lt;/</span><span class="n">body</span><span class="o">&gt;</span>
<span class="o">&lt;/</span><span class="n">html</span><span class="o">&gt;</span>
</pre></div>
</div>
<p>Example of changing the attribute &#8220;target&#8221; of every link in first paragraph:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">xml.etree.ElementTree</span> <span class="k">import</span> <span class="n">ElementTree</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tree</span> <span class="o">=</span> <span class="n">ElementTree</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">&quot;index.xhtml&quot;</span><span class="p">)</span>
<span class="go">&lt;Element &#39;html&#39; at 0xb77e6fac&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">&quot;body/p&quot;</span><span class="p">)</span>     <span class="c"># Finds first occurrence of tag p in body</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span>
<span class="go">&lt;Element &#39;p&#39; at 0xb77ec26c&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">links</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">iter</span><span class="p">(</span><span class="s">&quot;a&quot;</span><span class="p">))</span>   <span class="c"># Returns list of all links</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">links</span>
<span class="go">[&lt;Element &#39;a&#39; at 0xb77ec2ac&gt;, &lt;Element &#39;a&#39; at 0xb77ec1cc&gt;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">links</span><span class="p">:</span>             <span class="c"># Iterates through all found links</span>
<span class="gp">... </span>    <span class="n">i</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s">&quot;target&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&quot;blank&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tree</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;output.xhtml&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="qname-objects">
<span id="elementtree-qname-objects"></span><h3>20.5.3.4. QName Objects<a class="headerlink" href="#qname-objects" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="xml.etree.ElementTree.QName">
<em class="property">class </em><tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">QName</tt><big>(</big><em>text_or_uri</em>, <em>tag=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.QName" title="Permalink to this definition">¶</a></dt>
<dd><p>QName wrapper.  This can be used to wrap a QName attribute value, in order
to get proper namespace handling on output.  <em>text_or_uri</em> is a string
containing the QName value, in the form {uri}local, or, if the tag argument
is given, the URI part of a QName.  If <em>tag</em> is given, the first argument is
interpreted as an URI, and this argument is interpreted as a local name.
<a class="reference internal" href="#xml.etree.ElementTree.QName" title="xml.etree.ElementTree.QName"><tt class="xref py py-class docutils literal"><span class="pre">QName</span></tt></a> instances are opaque.</p>
</dd></dl>

</div>
<div class="section" id="treebuilder-objects">
<span id="elementtree-treebuilder-objects"></span><h3>20.5.3.5. TreeBuilder Objects<a class="headerlink" href="#treebuilder-objects" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="xml.etree.ElementTree.TreeBuilder">
<em class="property">class </em><tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">TreeBuilder</tt><big>(</big><em>element_factory=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder" title="Permalink to this definition">¶</a></dt>
<dd><p>Generic element structure builder.  This builder converts a sequence of
start, data, and end method calls to a well-formed element structure.  You
can use this class to build an element structure using a custom XML parser,
or a parser for some other XML-like format.  <em>element_factory</em>, when given,
must be a callable accepting two positional arguments: a tag and
a dict of attributes.  It is expected to return a new element instance.</p>
<dl class="method">
<dt id="xml.etree.ElementTree.TreeBuilder.close">
<tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.close" title="Permalink to this definition">¶</a></dt>
<dd><p>Flushes the builder buffers, and returns the toplevel document
element.  Returns an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> instance.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.TreeBuilder.data">
<tt class="descname">data</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.data" title="Permalink to this definition">¶</a></dt>
<dd><p>Adds text to the current element.  <em>data</em> is a string.  This should be
either a bytestring, or a Unicode string.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.TreeBuilder.end">
<tt class="descname">end</tt><big>(</big><em>tag</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.end" title="Permalink to this definition">¶</a></dt>
<dd><p>Closes the current element.  <em>tag</em> is the element name.  Returns the
closed element.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.TreeBuilder.start">
<tt class="descname">start</tt><big>(</big><em>tag</em>, <em>attrs</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.start" title="Permalink to this definition">¶</a></dt>
<dd><p>Opens a new element.  <em>tag</em> is the element name.  <em>attrs</em> is a dictionary
containing element attributes.  Returns the opened element.</p>
</dd></dl>

<p>In addition, a custom <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><tt class="xref py py-class docutils literal"><span class="pre">TreeBuilder</span></tt></a> object can provide the
following method:</p>
<dl class="method">
<dt id="xml.etree.ElementTree.TreeBuilder.doctype">
<tt class="descname">doctype</tt><big>(</big><em>name</em>, <em>pubid</em>, <em>system</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.doctype" title="Permalink to this definition">¶</a></dt>
<dd><p>Handles a doctype declaration.  <em>name</em> is the doctype name.  <em>pubid</em> is
the public identifier.  <em>system</em> is the system identifier.  This method
does not exist on the default <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><tt class="xref py py-class docutils literal"><span class="pre">TreeBuilder</span></tt></a> class.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="xmlparser-objects">
<span id="elementtree-xmlparser-objects"></span><h3>20.5.3.6. XMLParser Objects<a class="headerlink" href="#xmlparser-objects" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="xml.etree.ElementTree.XMLParser">
<em class="property">class </em><tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">XMLParser</tt><big>(</big><em>html=0</em>, <em>target=None</em>, <em>encoding=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.XMLParser" title="Permalink to this definition">¶</a></dt>
<dd><p>This class is the low-level building block of the module.  It uses
<a class="reference internal" href="pyexpat.html#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><tt class="xref py py-mod docutils literal"><span class="pre">xml.parsers.expat</span></tt></a> for efficient, event-based parsing of XML.  It can
be fed XML data incrementall with the <a class="reference internal" href="#xml.etree.ElementTree.XMLParser.feed" title="xml.etree.ElementTree.XMLParser.feed"><tt class="xref py py-meth docutils literal"><span class="pre">feed()</span></tt></a> method, and parsing events
are translated to a push API - by invoking callbacks on the <em>target</em> object.
If <em>target</em> is omitted, the standard <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><tt class="xref py py-class docutils literal"><span class="pre">TreeBuilder</span></tt></a> is used.  The
<em>html</em> argument was historically used for backwards compatibility and is now
deprecated.  If <em>encoding</em> <a class="footnote-reference" href="#id5" id="id4">[1]</a> is given, the value overrides the encoding
specified in the XML file.</p>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 3.4: </span>The <em>html</em> argument.  The remaining arguments should be passed via
keywword to prepare for the removal of the <em>html</em> argument.</p>
</div>
<dl class="method">
<dt id="xml.etree.ElementTree.XMLParser.close">
<tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.XMLParser.close" title="Permalink to this definition">¶</a></dt>
<dd><p>Finishes feeding data to the parser.  Returns the result of calling the
<tt class="docutils literal"><span class="pre">close()</span></tt> method of the <em>target</em> passed during construction; by default,
this is the toplevel document element.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.XMLParser.doctype">
<tt class="descname">doctype</tt><big>(</big><em>name</em>, <em>pubid</em>, <em>system</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.XMLParser.doctype" title="Permalink to this definition">¶</a></dt>
<dd><div class="deprecated">
<p><span class="versionmodified">Deprecated since version 3.2: </span>Define the <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder.doctype" title="xml.etree.ElementTree.TreeBuilder.doctype"><tt class="xref py py-meth docutils literal"><span class="pre">TreeBuilder.doctype()</span></tt></a> method on a custom TreeBuilder
target.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.XMLParser.feed">
<tt class="descname">feed</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.XMLParser.feed" title="Permalink to this definition">¶</a></dt>
<dd><p>Feeds data to the parser.  <em>data</em> is encoded data.</p>
</dd></dl>

<p><a class="reference internal" href="#xml.etree.ElementTree.XMLParser.feed" title="xml.etree.ElementTree.XMLParser.feed"><tt class="xref py py-meth docutils literal"><span class="pre">XMLParser.feed()</span></tt></a> calls <em>target</em>&#8216;s <tt class="docutils literal"><span class="pre">start(tag,</span> <span class="pre">attrs_dict)</span></tt> method
for each opening tag, its <tt class="docutils literal"><span class="pre">end(tag)</span></tt> method for each closing tag, and data
is processed by method <tt class="docutils literal"><span class="pre">data(data)</span></tt>.  <a class="reference internal" href="#xml.etree.ElementTree.XMLParser.close" title="xml.etree.ElementTree.XMLParser.close"><tt class="xref py py-meth docutils literal"><span class="pre">XMLParser.close()</span></tt></a> calls
<em>target</em>&#8216;s method <tt class="docutils literal"><span class="pre">close()</span></tt>. <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a> can be used not only for
building a tree structure. This is an example of counting the maximum depth
of an XML file:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">xml.etree.ElementTree</span> <span class="k">import</span> <span class="n">XMLParser</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">MaxDepth</span><span class="p">:</span>                     <span class="c"># The target object of the parser</span>
<span class="gp">... </span>    <span class="n">maxDepth</span> <span class="o">=</span> <span class="mi">0</span>
<span class="gp">... </span>    <span class="n">depth</span> <span class="o">=</span> <span class="mi">0</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">start</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">attrib</span><span class="p">):</span>   <span class="c"># Called for each opening tag.</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">depth</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">depth</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxDepth</span><span class="p">:</span>
<span class="gp">... </span>            <span class="bp">self</span><span class="o">.</span><span class="n">maxDepth</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">depth</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tag</span><span class="p">):</span>             <span class="c"># Called for each closing tag.</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">depth</span> <span class="o">-=</span> <span class="mi">1</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">pass</span>            <span class="c"># We do not need to do anything with data.</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>    <span class="c"># Called when all data has been parsed.</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxDepth</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">target</span> <span class="o">=</span> <span class="n">MaxDepth</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span> <span class="o">=</span> <span class="n">XMLParser</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">target</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">exampleXml</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;</span>
<span class="gp">... </span><span class="s">&lt;a&gt;</span>
<span class="gp">... </span><span class="s">  &lt;b&gt;</span>
<span class="gp">... </span><span class="s">  &lt;/b&gt;</span>
<span class="gp">... </span><span class="s">  &lt;b&gt;</span>
<span class="gp">... </span><span class="s">    &lt;c&gt;</span>
<span class="gp">... </span><span class="s">      &lt;d&gt;</span>
<span class="gp">... </span><span class="s">      &lt;/d&gt;</span>
<span class="gp">... </span><span class="s">    &lt;/c&gt;</span>
<span class="gp">... </span><span class="s">  &lt;/b&gt;</span>
<span class="gp">... </span><span class="s">&lt;/a&gt;&quot;&quot;&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">feed</span><span class="p">(</span><span class="n">exampleXml</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="go">4</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="xmlpullparser-objects">
<span id="elementtree-xmlpullparser-objects"></span><h3>20.5.3.7. XMLPullParser Objects<a class="headerlink" href="#xmlpullparser-objects" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="xml.etree.ElementTree.XMLPullParser">
<em class="property">class </em><tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">XMLPullParser</tt><big>(</big><em>events=None</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.XMLPullParser" title="Permalink to this definition">¶</a></dt>
<dd><p>A pull parser suitable for non-blocking applications.  Its input-side API is
similar to that of <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a>, but instead of pushing calls to a
callback target, <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLPullParser</span></tt></a> collects an internal list of parsing
events and lets the user read from it. <em>events</em> is a sequence of events to
report back.  The supported events are the strings <tt class="docutils literal"><span class="pre">&quot;start&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;end&quot;</span></tt>,
<tt class="docutils literal"><span class="pre">&quot;start-ns&quot;</span></tt> and <tt class="docutils literal"><span class="pre">&quot;end-ns&quot;</span></tt> (the &#8220;ns&#8221; events are used to get detailed
namespace information).  If <em>events</em> is omitted, only <tt class="docutils literal"><span class="pre">&quot;end&quot;</span></tt> events are
reported.</p>
<dl class="method">
<dt id="xml.etree.ElementTree.XMLPullParser.feed">
<tt class="descname">feed</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.XMLPullParser.feed" title="Permalink to this definition">¶</a></dt>
<dd><p>Feed the given bytes data to the parser.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.XMLPullParser.close">
<tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.XMLPullParser.close" title="Permalink to this definition">¶</a></dt>
<dd><p>Signal the parser that the data stream is terminated. Unlike
<a class="reference internal" href="#xml.etree.ElementTree.XMLParser.close" title="xml.etree.ElementTree.XMLParser.close"><tt class="xref py py-meth docutils literal"><span class="pre">XMLParser.close()</span></tt></a>, this method always returns <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const docutils literal"><span class="pre">None</span></tt></a>.
Any events not yet retrieved when the parser is closed can still be
read with <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="xml.etree.ElementTree.XMLPullParser.read_events"><tt class="xref py py-meth docutils literal"><span class="pre">read_events()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="xml.etree.ElementTree.XMLPullParser.read_events">
<tt class="descname">read_events</tt><big>(</big><big>)</big><a class="headerlink" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an iterator over the events which have been encountered in the
data fed to the
parser.  The iterator yields <tt class="docutils literal"><span class="pre">(event,</span> <span class="pre">elem)</span></tt> pairs, where <em>event</em> is a
string representing the type of event (e.g. <tt class="docutils literal"><span class="pre">&quot;end&quot;</span></tt>) and <em>elem</em> is the
encountered <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> object.</p>
<p>Events provided in a previous call to <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="xml.etree.ElementTree.XMLPullParser.read_events"><tt class="xref py py-meth docutils literal"><span class="pre">read_events()</span></tt></a> will not be
yielded again.  Events are consumed from the internal queue only when
they are retrieved from the iterator, so multiple readers iterating in
parallel over iterators obtained from <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="xml.etree.ElementTree.XMLPullParser.read_events"><tt class="xref py py-meth docutils literal"><span class="pre">read_events()</span></tt></a> will have
unpredictable results.</p>
</dd></dl>

<div class="admonition note">
<p class="first admonition-title">Note</p>
<p><a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLPullParser</span></tt></a> only guarantees that it has seen the &#8220;&gt;&#8221;
character of a starting tag when it emits a &#8220;start&#8221; event, so the
attributes are defined, but the contents of the text and tail attributes
are undefined at that point.  The same applies to the element children;
they may or may not be present.</p>
<p class="last">If you need a fully populated element, look for &#8220;end&#8221; events instead.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4.</span></p>
</div>
</dd></dl>

</div>
<div class="section" id="exceptions">
<h3>20.5.3.8. Exceptions<a class="headerlink" href="#exceptions" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="xml.etree.ElementTree.ParseError">
<em class="property">class </em><tt class="descclassname">xml.etree.ElementTree.</tt><tt class="descname">ParseError</tt><a class="headerlink" href="#xml.etree.ElementTree.ParseError" title="Permalink to this definition">¶</a></dt>
<dd><p>XML parse error, raised by the various parsing methods in this module when
parsing fails.  The string representation of an instance of this exception
will contain a user-friendly error message.  In addition, it will have
the following attributes available:</p>
<dl class="attribute">
<dt id="xml.etree.ElementTree.ParseError.code">
<tt class="descname">code</tt><a class="headerlink" href="#xml.etree.ElementTree.ParseError.code" title="Permalink to this definition">¶</a></dt>
<dd><p>A numeric error code from the expat parser. See the documentation of
<a class="reference internal" href="pyexpat.html#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><tt class="xref py py-mod docutils literal"><span class="pre">xml.parsers.expat</span></tt></a> for the list of error codes and their meanings.</p>
</dd></dl>

<dl class="attribute">
<dt id="xml.etree.ElementTree.ParseError.position">
<tt class="descname">position</tt><a class="headerlink" href="#xml.etree.ElementTree.ParseError.position" title="Permalink to this definition">¶</a></dt>
<dd><p>A tuple of <em>line</em>, <em>column</em> numbers, specifying where the error occurred.</p>
</dd></dl>

</dd></dl>

<p class="rubric">Footnotes</p>
<table class="docutils footnote" frame="void" id="id5" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[1]</td><td>The encoding string included in XML output should conform to the
appropriate standards.  For example, &#8220;UTF-8&#8221; is valid, but &#8220;UTF8&#8221; is
not.  See <a class="reference external" href="http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl">http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl</a>
and <a class="reference external" href="http://www.iana.org/assignments/character-sets/character-sets.xhtml">http://www.iana.org/assignments/character-sets/character-sets.xhtml</a>.</td></tr>
</tbody>
</table>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">20.5. <tt class="docutils literal"><span class="pre">xml.etree.ElementTree</span></tt> &#8212; The ElementTree XML API</a><ul>
<li><a class="reference internal" href="#tutorial">20.5.1. Tutorial</a><ul>
<li><a class="reference internal" href="#xml-tree-and-elements">20.5.1.1. XML tree and elements</a></li>
<li><a class="reference internal" href="#parsing-xml">20.5.1.2. Parsing XML</a></li>
<li><a class="reference internal" href="#pull-api-for-non-blocking-parsing">20.5.1.3. Pull API for non-blocking parsing</a></li>
<li><a class="reference internal" href="#finding-interesting-elements">20.5.1.4. Finding interesting elements</a></li>
<li><a class="reference internal" href="#modifying-an-xml-file">20.5.1.5. Modifying an XML File</a></li>
<li><a class="reference internal" href="#building-xml-documents">20.5.1.6. Building XML documents</a></li>
<li><a class="reference internal" href="#parsing-xml-with-namespaces">20.5.1.7. Parsing XML with Namespaces</a></li>
<li><a class="reference internal" href="#additional-resources">20.5.1.8. Additional resources</a></li>
</ul>
</li>
<li><a class="reference internal" href="#xpath-support">20.5.2. XPath support</a><ul>
<li><a class="reference internal" href="#example">20.5.2.1. Example</a></li>
<li><a class="reference internal" href="#supported-xpath-syntax">20.5.2.2. Supported XPath syntax</a></li>
</ul>
</li>
<li><a class="reference internal" href="#reference">20.5.3. Reference</a><ul>
<li><a class="reference internal" href="#functions">20.5.3.1. Functions</a></li>
<li><a class="reference internal" href="#element-objects">20.5.3.2. Element Objects</a></li>
<li><a class="reference internal" href="#elementtree-objects">20.5.3.3. ElementTree Objects</a></li>
<li><a class="reference internal" href="#qname-objects">20.5.3.4. QName Objects</a></li>
<li><a class="reference internal" href="#treebuilder-objects">20.5.3.5. TreeBuilder Objects</a></li>
<li><a class="reference internal" href="#xmlparser-objects">20.5.3.6. XMLParser Objects</a></li>
<li><a class="reference internal" href="#xmlpullparser-objects">20.5.3.7. XMLPullParser Objects</a></li>
<li><a class="reference internal" href="#exceptions">20.5.3.8. Exceptions</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="xml.html"
                        title="previous chapter">20.4. XML Processing Modules</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="xml.dom.html"
                        title="next chapter">20.6. <tt class="docutils literal"><span class="pre">xml.dom</span></tt> &#8212; The Document Object Model API</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/xml.etree.elementtree.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="xml.dom.html" title="20.6. xml.dom — The Document Object Model API"
             >next</a> |</li>
        <li class="right" >
          <a href="xml.html" title="20.4. XML Processing Modules"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="markup.html" >20. Structured Markup Processing Tools</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2015, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Aug 18, 2015.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
    </div>

  </body>
</html>